﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: Propietario			*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.Entity;
using System.Data;
using ESF.General;
using ESF.General.Text;
using ESF.SSO.Core.Entities;

#endregion


namespace ESF.SSO.Core.DataAccess.BasicReferences
{
	public static class AdoBasicReference
	{
		#region Write
		
		public static void RegisterTreeRelationsValue (List<BasicReference> pTreeItemsOrdered, string pValue)
		{
			IDbCommand cmd;

			
			if (DataHelper.IsEmpty (pTreeItemsOrdered)) { return; }

			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_RegisterTreeRelationsValue");

			if (pTreeItemsOrdered [0].ID != null)
			{			
				AdoESFSSO.W.AddINIDsParameter (cmd, "pOrderedRelationIDList", false, pTreeItemsOrdered);
			}
			else
			{
				AdoESFSSO.W.AddINCodesParameter (cmd, "pOrderedRelationCodeList", false, pTreeItemsOrdered);
				AdoESFSSO.W.AddINTypeIDsParameter (cmd, "pOrderedBasicReferencesTypeIDsList", false, pTreeItemsOrdered);
			}
				
			AdoESFSSO.W.AddParameter (cmd, "pValue", pValue);
			
			AdoESFSSO.W.Execute (cmd);
		}


		public static void EnsureTreeItem (ref TreeBasicReference pTreeItem)
		{
			if (pTreeItem == null) { return; }

			if (pTreeItem.ID == null)
			{
				if (pTreeItem.Parent.ID == null)
				{
					pTreeItem.Parent.ID = RegisterIfNotExists (pTreeItem.Parent);
				}

				if (pTreeItem.Child != null)
				{
					if (pTreeItem.Child.ID == null && !string.IsNullOrEmpty (pTreeItem.Child.Name))
					{
						pTreeItem.Child.ID = RegisterIfNotExists (pTreeItem.Child);
					}
				}

				pTreeItem.ID = RegisterByIDs (pTreeItem);
			}		
		}


		public static void DeleteChildrenBasicReference<BR> (BR pBigParentBasRef, BR pBasRef, int? pBasicReferenceTypeID) where BR : BasicReference
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_DeleteBasicReference_BasicReferences");

			AdoESFSSO.W.AddIDParameter (cmd, "pBasicReferenceID", pBasRef);
			AdoESFSSO.W.AddIDParameter (cmd, "pBigParentBasicReferenceID", pBigParentBasRef);
			AdoESFSSO.W.AddIDParameter (cmd, "pChildrenBasicReferenceTypeID", pBasicReferenceTypeID);
			
			AdoESFSSO.W.Execute (cmd);
		}


		public static int? RegisterByCodes (TreeBasicReference pItem)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_InsertTreeBasicReferenceCodes");

			AdoESFSSO.W.AddIDParameter (cmd, "pParentBasicReferenceTypeID", pItem.Parent.BasicReferenceType.ID);
			AdoESFSSO.W.AddCodeParameter (cmd, "pParentCode", pItem.Parent);
			
			AdoESFSSO.W.AddIDParameter (cmd, "pChildBasicReferenceTypeID", pItem.Child.BasicReferenceType.ID);
			AdoESFSSO.W.AddCodeParameter (cmd, "pChildCode", pItem.Child);
			
			AdoESFSSO.W.AddOutParameter (cmd, "pID", DbType.Int32);

			AdoESFSSO.W.Execute (cmd);

			return AdoESFSSO.W.GetParameterValue <int> (cmd, "pID");
		}


		public static int? RegisterByIDs (TreeBasicReference pItem)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_InsertTreeBasicReferenceIDs");

			AdoESFSSO.W.AddIDParameter (cmd, "pParentID", pItem.Parent);
			AdoESFSSO.W.AddIDParameter (cmd, "pChildID", pItem.Child);
			AdoESFSSO.W.AddOutParameter (cmd, "pID", DbType.Int32);

			AdoESFSSO.W.Execute (cmd);

			return AdoESFSSO.W.GetParameterValue <int> (cmd, "pID");
		}


		public static int? UnRegisterByIDs (TreeBasicReference pItem)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_DeleteTreeBasicReferenceIDs");

			AdoESFSSO.W.AddIDParameter (cmd, "pParentID", pItem.Parent);
			AdoESFSSO.W.AddIDParameter (cmd, "pChildID", pItem.Child);
			AdoESFSSO.W.AddIDParameter (cmd, "pID", pItem.ID);

			AdoESFSSO.W.Execute (cmd);

			return AdoESFSSO.W.GetParameterValue <int> (cmd, "pID");
		}


		public static void Delete (BasicReference pBR)
		{	
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_DeleteBasicReference");
			
			AdoESFSSO.W.AddIDParameter (cmd, "pBasicReferenceID", pBR.ID);
			
			AdoESFSSO.W.Execute (cmd);
		}

		
		public static int RegisterLocated<BR> (BR pBR) where BR : LocatedBasicReference
		{
			AdoLocation.RegisterLocation (pBR);

			return Register<BR> (pBR);
		}
		
		
		public static int? RegisterIfNotExists<BR> (BR pBR) where BR : BasicReference
		{
			BasicReference temp;


			if (!Validate.IsFilledID (pBR.ID))
			{
				temp = Get (pBR);

				if (temp != null)
				{
					return temp.ID;
				}
			}

			return Register (pBR);
		}


		public static int Register<BR> (BR pBR) where BR : BasicReference
		{
			IDbCommand cmd;


			Validate.FilledObject(true, "Reference Name", pBR.Name);
			Validate.FilledObject(true, "BasicReferenceType", pBR.BasicReferenceType);
			Validate.FilledObject(true, "BasicReferenceType ID", pBR.BasicReferenceType.ID); 
			Validate.FilledObject(true, "Reference", pBR);
			Validate.FilledObject (true, "Reference Enabled", pBR.IsEnabled);
			

			if (TextHelper.IsEmpty (pBR.Code))
			{
				pBR.Code = Guid.NewGuid ().ToString ();
			}
					

			if (!Validate.IsFilledID (pBR.ID))
			{
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_InsertBasicReference");
				
				AdoESFSSO.W.AddOutParameter (cmd, "pBasicReferenceID", DbType.Int32);
				AdoESFSSO.W.AddAuditableEntityParamsCreation (cmd, pBR);
			}
			else
			{
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_UpdateBasicReference");
				
				AdoESFSSO.W.AddParameter (cmd, "pBasicReferenceID", pBR.ID);
				AdoESFSSO.W.AddAuditableEntityParamsUpdate (cmd, pBR);
			}			
			
			AdoESFSSO.W.AddParameter (cmd, "pBasicReferenceTypeID", pBR.BasicReferenceType.ID);
			AdoESFSSO.W.AddParameter (cmd, "pCode", pBR.Code);
			AdoESFSSO.W.AddParameter (cmd, "pName", pBR.Name);
			AdoESFSSO.W.AddParameter (cmd, "pExtra_I", pBR.Extra_I);
			AdoESFSSO.W.AddParameter (cmd, "pExtra_II", pBR.Extra_II);
			AdoESFSSO.W.AddParameter (cmd, "pExtra_III", pBR.Extra_III);
			AdoESFSSO.W.AddParameter (cmd, "pDescription", pBR.Description);
			AdoESFSSO.W.AddParameter (cmd, "pIsEnabled", pBR.IsEnabled);
			
			AdoESFSSO.W.Execute (cmd);
			
			return AdoESFSSO.W.GetParameterValue <int> (cmd, "pBasicReferenceID");
		}
		
		#endregion


		#region Words

		public static int GetOrRegisterSimilarWord(string pWord)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.W.CreateSPCommand("esf_sso.P_GetOrRegisterSimilarWord");

			AdoESFSSO.W.AddParameter(cmd, "pWord", pWord);
			AdoESFSSO.W.AddOutParameter(cmd, "pWordID", DbType.Int32);

			AdoESFSSO.W.Execute(cmd);

			return AdoESFSSO.W.GetParameterValue<int>(cmd, "pWordID");
		}

		#endregion

		
		#region Read

		public static bool ExistsBasicReference(BasicReference pItem)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_ExistsBasicReference");

			AdoESFSSO.R.AddIDParameter (cmd, "pID", pItem);
			AdoESFSSO.R.AddCodeParameter (cmd, "pCode", pItem);
			AdoESFSSO.R.AddIDParameter(cmd, "pBasicReferenceTypeID", pItem.BasicReferenceType == null ? pItem.BasicReferenceTypeID : pItem.BasicReferenceType.ID);

			return AdoESFSSO.R.HasRows (cmd);
		}


		public static BasicReference GetParentTreeBasicReference(BasicReference pChild, BasicReferenceType pBasicReferenceType)
		{
			return GetParentTreeBasicReference (pChild, pBasicReferenceType.ID);
		}


		public static BasicReference GetParentTreeBasicReference (BasicReference pChild, int pBasicReferenceTypeID)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetParentTreeBasicReference");

			AdoESFSSO.R.AddIDParameter(cmd, "pBasicReferenceTypeID", pBasicReferenceTypeID);
			AdoESFSSO.R.AddIDParameter(cmd, "pBasicReferenceID", pChild);

			return AdoESFSSO.R.Get<BasicReference> (cmd, DataHelper.ConvertToDirectBasicReference);
		}


		public static List<BasicReference> GetChildrenTreeBasicReference (List<BasicReference> pParentBasicReferences, int? pBasicReferenteTypeID)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetChildrenTreeBasicReference");
			
			AdoESFSSO.R.AddINIDsParameter (cmd, "pParentBasicReferenceIDs", false, pParentBasicReferences);
			AdoESFSSO.R.AddIDParameter (cmd, "pBasicReferenceTypeID", pBasicReferenteTypeID);

			return AdoESFSSO.R.GetList <BasicReference> (cmd, DataHelper.ConvertToDirectBasicReference);
		}


		public static List<BasicReference> GetChildrenBasicReferences (BasicReference pParent, int pChildrenBasicReferenceTypeID)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetBasicReference_BasicReferences");

			AdoESFSSO.R.AddIDParameter (cmd, "pBasicReferenceID", pParent);
			AdoESFSSO.R.AddIDParameter (cmd, "pChildrenBasicReferenceTypeID", pChildrenBasicReferenceTypeID);

			return AdoESFSSO.R.GetList <BasicReference> (cmd, DataHelper.ConvertToDirectBasicReference);
		}

		
		//public static List<FileReference> GetFiles (BasicReference pBR)
		//{
		//    IDbCommand cmd;


		//    cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetBasicReferenceFiles");

		//    AdoESFSSO.R.AddIDParameter (cmd, "pBasicReferenceID", pBR);

		//    return new List<FileReference> ();
		//    //return AdoESFSSO.R.GetList <FileReference> (cmd, DataHelper.ConvertToFileReference);
		//}


		public static List<BasicReference> ListEnabled (int pBasicReferenceTypeID, int? pListCount = null)
		{
			return List (new BasicReference () { BasicReferenceType = new BasicReferenceType () { ID = pBasicReferenceTypeID }, IsEnabled = true }, pListCount);
		}


		public static List<BasicReference> List (BasicReference pSample, int? pListCount = null)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_ListBasicReferences");
			
			AdoESFSSO.R.AddParameter (cmd, "pPartName", pSample.Name, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pListCount", pListCount);
			AdoESFSSO.R.AddParameter (cmd, "pIsEnabled", pSample.IsEnabled);
			
			if (pSample.filBasicReferenceTypeIDs == null)
			{
				if (pSample.BasicReferenceType != null)
				{
					AdoESFSSO.R.AddINParameter (cmd, "pBasicReferenceTypeIDs", false, pSample.BasicReferenceType.ID);
				}
			}
			else
			{
				AdoESFSSO.R.AddINParameter (cmd, "pBasicReferenceTypeIDs", false, pSample.filBasicReferenceTypeIDs);
			}
			
			return AdoESFSSO.R.GetList <BasicReference> (cmd, DataHelper.ConvertToBasicReference<BasicReference>);
		}
			
		
		public static BrowseResults<BasicReference> Browse (BasicReference pSample, PaggingInfo pPagInfo)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_BrowseBasicReferences");
			
			
			if (pSample.filBasicReferenceTypeIDs == null)
			{
				if (pSample.BasicReferenceType != null)
				{
					AdoESFSSO.R.AddINParameter (cmd, "pBasicReferenceTypeIDs", false, pSample.BasicReferenceType.ID);
				}
			}
			else
			{
				AdoESFSSO.R.AddINParameter (cmd, "pBasicReferenceTypeIDs", false, pSample.filBasicReferenceTypeIDs);
			}

			AdoESFSSO.R.AddParameter (cmd, "pCode", pSample.Code, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pName", pSample.Name, ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter (cmd, "pIsEnabled", pSample.IsEnabled);
			AdoESFSSO.R.AddParametersPagging (cmd, pPagInfo);
			
			return AdoESFSSO.R.GetBrowse<BasicReference> (cmd, DataHelper.ConvertToBasicReference<BasicReference>, pPagInfo);
		}


		public static BasicReference Get(BasicReference pSample)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetBasicReference");
			
			AdoESFSSO.R.AddIDParameter (cmd, "pID", pSample.ID);
			AdoESFSSO.R.AddParameter(cmd, "pName", pSample.Name);
			AdoESFSSO.R.AddParameter(cmd, "pCode", pSample.Code);

			if (pSample.BasicReferenceType != null)
			{
				AdoESFSSO.R.AddParameter(cmd, "pBasicReferenceTypeID", pSample.BasicReferenceType.ID);
			}
			
			return AdoESFSSO.R.Get <BasicReference> (cmd, DataHelper.ConvertToDirectBasicReference);
		}


		public static LocatedBasicReference Get(LocatedBasicReference pSample)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetLocatedBasicReference");
			
			AdoESFSSO.R.AddIDParameter (cmd, "pID", pSample.ID);
			AdoESFSSO.R.AddParameter(cmd, "pName", pSample.Name);
			AdoESFSSO.R.AddParameter(cmd, "pCode", pSample.Code);

			if (pSample.BasicReferenceType != null)
			{
				AdoESFSSO.R.AddParameter(cmd, "pBasicReferenceTypeID", pSample.BasicReferenceType.ID);
			}
			
			return AdoESFSSO.R.Get <LocatedBasicReference> (cmd, ConvertToLocated<LocatedBasicReference>);
		}
		
		
		public static LBR ConvertToLocated<LBR> (IDataReader pReader, string pPrefix = "", string pListLoading = "") where LBR : LocatedBasicReference, new ()
		{
			LBR result;
		
		
			result = DataHelper.ConvertToBasicReference<LBR> (pReader, pPrefix);
			
			//result.Files = GetFiles (result).ToList ();
			
			result.Location = AdoLocation.ConvertTo (pReader, pPrefix);
			
			return result;
		}
		
		#endregion
	}
}
